﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using MX.Core.Extensions;

namespace MX.Core.Caching.Memcached
{
    /// <summary>
    /// 采用memcached实现ICachingFactory接口的实例
    /// </summary>
    public class MemcachedCachingFactory : ICachingFactory
    {
        /// <summary>
        /// 获取缓存实例
        /// </summary>
        /// <returns></returns>
        public ICaching Default
        {
            get
            {
                return MemcachedCaching.Default;
            }
        }

        /// <summary>
        /// 获取某个名称的缓存实例
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ICaching CreateCaching(string name)
        {
            ICaching caching;
            if (!_cacheInstances.TryGetValue(name, out caching))
            {
                lock (_cacheInstances)
                {
                    if (!_cacheInstances.TryGetValue(name, out caching))
                    {

                        caching = new MemcachedCaching(name);
                        _cacheInstances.Add(name, caching);
                    }
                }
            }
            return caching;
        }

        /// <summary>
        /// 缓存实例
        /// </summary>
        private Dictionary<string, ICaching> _cacheInstances = new Dictionary<string, ICaching>();
    }
}
